package gitlet;

import org.junit.Before; // 使用 @Before 代替 @Test 的 setUp
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import static gitlet.Repository.GITLET_DIR;
import static gitlet.Repository.GITLET_DIR_OBJECT;
import static org.junit.Assert.*;

public class CommitTest {

    public static boolean deleteDirectory(File directory) {
        if (!directory.exists()) {
            return false; // 如果文件夹不存在，返回 false
        }

        // 如果是目录，递归删除内部文件和子目录
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 递归调用删除子文件或子目录
                    deleteDirectory(file);
                }
            }
        }
        return directory.delete();
    }

    @Before

    public void setup(){
        if(Repository.GITLET_DIR.exists()){
            deleteDirectory(Repository.GITLET_DIR);
        };
        GITLET_DIR.mkdir();
        GITLET_DIR_OBJECT.mkdir();
    }
    @Test
    public void testCommitFirst() {
        HashMap<String, String> new_Hashmap_add = new HashMap<>();
        ArrayList<String> file_rm = new ArrayList<String>();
        Commit initialCommit = new Commit("first commit", null, null, "00:00",
                new_Hashmap_add, file_rm);
        initialCommit.saveCommit();
        Commit initialCommit_copy = Commit.fromFile("8b6ec98afad30dfff9475caba2e43b4150164eac");

        assertEquals(initialCommit_copy.getParent(), initialCommit.getParent());
        assertEquals(initialCommit_copy.getMessage(), initialCommit.getMessage());
        assertEquals(initialCommit_copy.getCommitID(), initialCommit.getCommitID());

        HashMap<String, String> Hashmap_add = new HashMap<>();
        Hashmap_add.put("file1.txt", "6789wji");

        Commit secondCommit = new Commit("second commit", "8b6ec98afad30dfff9475caba2e43b4150164eac", null, "00:11",
                Hashmap_add, file_rm);
        secondCommit.saveCommit();
        Commit secondCommit_copy = Commit.fromFile("8a21dceb202ff63e8343863e33b60f70e29247fb");
        assertEquals(initialCommit_copy.getMessage(), initialCommit.getMessage());

    }


}